<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Size short summary.
 *
 * Size description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Size_model extends CI_Model
{
    public $size_id;
    public $title;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    //插入新尺码
    public function insert_size($sizeModel)
    {
        return $this->db->insert('size',$sizeModel);
    }

    //由尺码id 获取尺码
    public function get_size_by_id($size_id)
    {
        return $this->db->select()->from('size')->where(array('size_id'=>$size_id))->get()->row();
    }

    //获取所有尺码
    public function get_allsize()
    {
        return $this->db->select()->from('size')->get()->result();
    }



    //更新尺码
    public function edit_size($sizeModel)
    {

        return  $this->db->update('size',$sizeModel,array('size_id'=>$sizeModel->size_id));

    }

    //删除尺码
    public function del_size_byid($size_id)
    {
        $this->db->trans_begin();
        $this->db->delete('size',array('size_id'=>$size_id));
        $this->db->delete('goods_inventory',array('goods_size_id'=>$size_id));
        $this->db->delete('goods_size',array('size_id'=>$size_id));
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }

    //批量删除尺码
    public function batchdel_size($size_ids)
    {
        $this->db->trans_begin();
        foreach($size_ids as $sizeiditem)
        {
            $this->db->delete('size',array('size_id'=>$sizeiditem));
            $this->db->delete('goods_inventory',array('goods_size_id'=>$sizeiditem));
            $this->db->delete('goods_size',array('size_id'=>$sizeiditem));
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }



    /**
     * @param mixed $cpage
     * @param mixed $pagesize
     * @param mixed $files
     * @param mixed $tabel
     * @param mixed $order
     */
    public function get_page($cpage,$pagesize,$files,$tabel,$order,$temp_id,$where)
    {
        $sql="SELECT $files FROM $tabel a JOIN (select $temp_id from $tabel $where limit ".($cpage-1)*$pagesize.", $pagesize) b ON a.$temp_id = b.$temp_id ";
        $sql_count="select count('$temp_id') as _count from $tabel $where";
        $data['objlist']=$this->db->query($sql)->result();
        $data['count']=$this->db->query($sql_count)->result();
        return $data;
    }

    // 获取全部尺码以及 商品尺码 left join 查询
    public function get_size_goods_size($goodid)
    {
        $sql="select s.size_id as sizeid,s.title as title,g.goods_size_id  from size s LEFT JOIN goods_size g on s.size_id=g.size_id and g.goods_id=$goodid";
        return $this->db->query($sql)->result_array();
    }
}